package com.example.wechat.dialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.example.wechat.R;
import com.example.wechat.model.PaymentResult;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

public class PaymentResultDialog extends Dialog {
    private PaymentResult result;
    private SimpleDateFormat dateFormat;

    public PaymentResultDialog(@NonNull Context context, PaymentResult result) {
        super(context);
        this.result = result;
        this.dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault());
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_payment_result);

        // 设置对话框宽度为屏幕宽度的80%
        getWindow().setLayout(
            (int) (getContext().getResources().getDisplayMetrics().widthPixels * 0.8),
            android.view.WindowManager.LayoutParams.WRAP_CONTENT
        );

        // 初始化视图
        ImageView resultIcon = findViewById(R.id.result_icon);
        TextView resultText = findViewById(R.id.result_text);
        TextView amountText = findViewById(R.id.amount_text);
        TextView timeText = findViewById(R.id.time_text);
        TextView orderNumberText = findViewById(R.id.order_number_text);
        Button confirmButton = findViewById(R.id.confirm_button);

        // 设置支付结果信息
        resultIcon.setImageResource(R.drawable.ic_success);
        resultText.setText("支付成功");
        amountText.setText(String.format("¥%.2f", result.getAmount()));
        timeText.setText(dateFormat.format(new Date(result.getTime())));
        orderNumberText.setText(String.format("订单号：%s", result.getOrderNumber()));

        // 设置确认按钮点击事件
        confirmButton.setOnClickListener(v -> dismiss());
    }
} 